function random(min,max){
	return min+Math.floor(Math.random()*(max-min))
}
class Plane{
	constructor(x,y,src){
		this.x = x;
		this.y = y;
		this.width=66;
		this.height=80;
		this.node = document.createElement("img");
		this.node.src = "image/my.gif";
	}
	moveTo(x,y){
		this.x = x;
		this.y = y;
		this.node.style.left = this.x+"px";
		this.node.style.top = this.y+"px";
	}
}
class MyPlane extends Plane{
	shoot(){
		var b = new Bullet(this.x+this.width/2-3,this.y-this.width/10);
		bullets.push(b)
		document.getElementById("main").appendChild(b.node);
	}
}
class Enemy extends Plane{
	constructor(x,y,src,speed,width,height){
		super(0,0);
		this.node.src = src;
		this.hp = 1;
		this.width=width;
		this.height=height;
		y-=this.height;
		x-=this.width/2;
		this.moveTo(x,y);
		this.speed=speed;
	}
	moveDown(){
		this.y+=this.speed;
		this.node.style.top = this.y+"px";
	}
}
class Bullet{
	constructor(x,y){
		this.x = x;
		this.y = y;
		this.width=6;
		this.height=14;
		this.node = document.createElement("img");
		this.node.src = "image/bullet1.png";
		this.node.style.left = this.x+"px";
		this.node.style.top = this.y+"px";
	}
	fly(){
		this.y-=10;
		this.node.style.top = this.y+"px";
	}
}
class Game{
	constructor(){
		var p = new MyPlane();
		this.width=320;
		this.height=568;
		this.p = p
		p.moveTo((this.width-p.width)/2,this.height-p.height);
		document.getElementById("main").appendChild(p.node);
		document.getElementById("main").addEventListener("mousemove",function(){
			p.moveTo(event.x-33,event.y-40)
		});
		
	}
	fresh(){
			frames++;
			if (frames%5==0) {
				this.p.shoot()
			}
			
			for (var i = 0; i < bullets.length; i++) {
				bullets[i].fly();
				if (bullets[i].y<0) {
					document.getElementById("main").removeChild(bullets[i].node);
					bullets.splice(i,1);
				}
				for (var j = 0; j < enemies.length; j++) {
					if (bullets[i].x >= enemies[j].x && bullets[i].x <= enemies[j].x+enemies[j].width) {
						if (bullets[i].y>=enemies[j].y && bullets[i].y <= enemies[j].y+enemies[j].height) {
							document.getElementById("main").removeChild(enemies[j].node);
							enemies.splice(j,1);
						}
					}
				}
			}
			var r = Math.floor(Math.random()*320);
			if (frames%20==0) {
				var e = new Enemy(r,0,"image/enemy1_fly_1.png",random(3,5),34,24);
				enemies.push(e);
				document.getElementById("main").appendChild(e.node);
			}// 小飞机
			
			
			if (frames%200==0) {
				var e = new Enemy(r,0,"image/enemy2_fly_1.png",random(1,2),110,164);
				enemies.push(e);
				document.getElementById("main").appendChild(e.node);
			}// 超大飞机
			
			
			if (frames%50==0) {
				var e = new Enemy(r,0,"image/enemy3_fly_1.png",random(2,3),46,60);
				enemies.push(e);
				document.getElementById("main").appendChild(e.node);
			}//大飞机
			
			for (var i = 0; i < enemies.length; i++) {
				enemies[i].moveDown();
				if (enemies[i].y>568) {
					document.getElementById("main").removeChild(enemies[i].node);
					enemies.splice(i,1);
				}
			}
			
		}
}

var enemies = [];
var bullets = [];
var frames = 0;
var g = new Game();
setInterval(function(){
			g.fresh()
			},20)